package org.jeecg.modules.demo.viporderrecord.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.jeecg.common.util.TokenUtils;
import org.jeecg.common.util.oConvertUtils;
import org.jeecg.modules.demo.castcardrecord.service.ICastCardRecordService;
import org.jeecg.modules.demo.viporderrecord.entity.VipOrderRecord;
import org.jeecg.modules.demo.viporderrecord.entity.VipOrderRecordVo;
import org.jeecg.modules.demo.viporderrecord.service.IVipOrderRecordService;
import org.jeecg.modules.util.RedisUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Description: 贵宾卡支付订单
 * @Author: jeecg-boot
 * @Date: 2022-01-14
 * @Version: V1.0
 */
@Api(tags = "贵宾卡支付订单")
@RestController
@RequestMapping("/viporderrecord")
@Slf4j
public class VipOrderRecordController extends JeecgController<VipOrderRecord, IVipOrderRecordService> {
    @Autowired
    private IVipOrderRecordService vipOrderRecordService;

    @Autowired
    private ICastCardRecordService castCardRecordService;

    @Autowired
    private RedisUtils redisUtils;

    /**
     * 分页列表查询
     */
    @AutoLog(value = "贵宾卡-分页列表查询")
    @ApiOperation(value = "股东管理-分页列表查询", notes = "股东管理-分页列表查询")
    @GetMapping(value = "/list")
    public Result<?> queryPageList(
            @RequestParam(name = "orderCode", defaultValue = "") String orderCode,
            @RequestParam(name = "cardName", defaultValue = "") String cardName,
            @RequestParam(name = "memberName", defaultValue = "") String memberName,
            @RequestParam(name = "memberPhone", defaultValue = "") String memberPhone,
            @RequestParam(name = "startDate", defaultValue = "") String startDate,
            @RequestParam(name = "endDate", defaultValue = "") String endDate,
            @RequestParam(name = "pageNo", defaultValue = "1") Integer pageNo,
            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize,
            HttpServletRequest req) {
        String token = TokenUtils.getTokenByRequest(req);
        String storeId = (String) redisUtils.get(RedisUtils.storeId + token);
        if (oConvertUtils.isEmpty(storeId)) {
            return Result.error("请先到首页选择店铺");
        }
        if (storeId.contains("过期")) {
            return Result.error("店铺已过期，续费后才可操作");
        }
        memberPhone = memberPhone.replaceAll("\\*","");
        orderCode = orderCode.replaceAll("\\*","");
        int total = vipOrderRecordService.countEx(storeId, orderCode, cardName, memberName, memberPhone, startDate, endDate);
        Map<String, Object> result = new HashMap<>();
        result.put("current", 1);
        result.put("total", total);

        List<VipOrderRecordVo> list = vipOrderRecordService.listEx(storeId, orderCode, cardName, memberName, memberPhone, startDate, endDate, pageNo, pageSize);

        result.put("records", list);
        return Result.OK(result);
    }
}
